import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.io.Console;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import objects.*;

import com.mdimension.jchronic.Chronic;
import com.mdimension.jchronic.Options;
import com.mdimension.jchronic.utils.Span;


public class FormatProcessor {
	//static Pattern addRegex = Pattern.compile("^-a\\s+\\w");
	static Pattern searchRegex = Pattern.compile("^search\\s+\\w|^-s\\s+\\w");
	static Pattern deleteRegex = Pattern.compile("^delete\\s+\\w|^-d\\s+\\w");
	static Pattern editRegex = Pattern.compile("^edit\\s+\\w|^-e\\s+\\w");
	
	//future implementation
	static Pattern addSubTaskRegex = Pattern.compile("^addsubtask\\s+\\w|^-as\\s+\\w"); 
	static Pattern priorityRegex = Pattern.compile("!\\d$|!!$");
	
	static Pattern datePeriodRegex = Pattern.compile("(from|-sd)\\s+.+(till|until|to|-ed)\\s+.+");
	static Pattern dateAfterRegex = Pattern.compile("after\\s+.+");
	static Pattern dateWithinRegex = Pattern.compile("within\\s+.+"); //experimental
	
	
	private Date[] parseDate(String input)
	{
		Matcher checkMatch;
		String matchedString;
		Calendar startDate;
		Calendar endDate;
		Date[] resultDates = new Date[2];
		
		checkMatch = datePeriodRegex.matcher(input);
		if(checkMatch.matches())
		{
			matchedString = checkMatch.group();
			String[] twoDates = matchedString.split("(till|until|to|-ed)");
			startDate = Chronic.parse(twoDates[0]).getBeginCalendar();
			endDate = Chronic.parse(twoDates[1]).getEndCalendar();
			if(startDate != null)
			{
				resultDates[0] = startDate.getTime();
			}
			if(startDate != null)
			{
				resultDates[0] = startDate.getTime();
			}
			
			return resultDates;
		}
		checkMatch = dateAfterRegex.matcher(input);
		if(checkMatch.matches())
		{
			matchedString = checkMatch.group();
			startDate = Chronic.parse(matchedString).getBeginCalendar();
			resultDates[0] = startDate.getTime();
			return resultDates;
			
		}
		
		endDate = Chronic.parse(input).getBeginCalendar();
		resultDates[0] = endDate.getTime();
		return resultDates;

	}
	
	public static Command processUserInput(String input)
	{

		Matcher checkMatch;
		//ArrayList<String> result = new ArrayList<String>();
		Command result = new Command();
		Date[] parsedDate;
		
		// Generally, the type of operation will occupy the first index of arraylist result[0]
		// The content will occupy the second index of arraylist result[1]
		// For Edit, the details of task to be edit will be located
		// at the third index result[2]
		checkMatch = searchRegex.matcher(input);
		if(checkMatch.matches())
		{
			input.replaceFirst("^search\\s+\\w|^-s\\s+\\w", "");

			result.setCommandType("SEARCH");
			result.setDetail(input.trim());
			
			return result;
		}
		checkMatch = deleteRegex.matcher(input);
		if(checkMatch.matches())
		{
			input.replaceFirst("^search\\s+\\w|^-s\\s+\\w", "");
			input.trim();
			
			result.setCommandType("DELETE");
			result.setDetail(input.trim());
			
			return result;
		}
		checkMatch = editRegex.matcher(input);
		if(checkMatch.matches())
		{
			input.replaceFirst("^search\\s+\\w|^-s\\s+\\w", "");
			
			String[] content = input.split("$%");
			
			//parsedDate = parseDate(input);
			//result.setStartDate(parsedDate[0]);
			//result.setEndDate(parsedDate[1]);
			
			result.setCommandType("EDIT");
			result.setParent(content[0].trim());
			result.setDetail(content[1].trim());
			return result;
		}

		//By default identify command as ADD
		result.setCommandType("ADD");
		result.setDetail(input.trim());
		//parsedDate = parseDate(input);
		//result.setStartDate(parsedDate[0]);
		//result.setEndDate(parsedDate[1]);
		
		return result;
	}
	
	public static void main(String[] args) {
	Console console = System.console();
	   if (console == null) {
	       System.err.println("No console.");
	       System.exit(1);
	   }
	   while (true) {

	       Pattern pattern = 
	       Pattern.compile(console.readLine("%nEnter your regex: "));

	       Matcher matcher = 
	       pattern.matcher(console.readLine("Enter input string to search: "));

	       boolean found = false;
	       while (matcher.find()) {
	           console.format("I found the text \"%s\" starting at " +
	              "index %d and ending at index %d.%n",
	               matcher.group(), matcher.start(), matcher.end());
	           found = true;
	       }
	       if(!found){
	           console.format("No match found.%n");
	       }
	   }
	}
	
	
}







